package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import metier.client.MetierFactory;
import metier.interfaces.IRemoteConnexion;
import metier.interfaces.IRemotePanier;

public class ActionConnexion implements IAction {

	@Override
	public String execute(HttpServletRequest req) throws Exception {

		String retour = "connexion.jsp";

		HttpSession session = req.getSession();

		boolean connected = false;

		IRemoteConnexion metier = (IRemoteConnexion) MetierFactory
				.getInstance().getMetier("Connexion");

		/* logout */
		if (req.getParameter("logout") != null) {
			System.out.println("blah");
			session.invalidate();
			session = req.getSession();
		}

		// test si non connecté
		if (session.getAttribute("id_client") == null) {

			System.out.println("");

			req.setAttribute("nom", "");
			req.setAttribute("prenom", "");
			req.setAttribute("numero", "");
			req.setAttribute("adresse", "");
			req.setAttribute("codepostal", "");
			req.setAttribute("ville", "");

			/* connexion */
			if ((req.getParameter("connexion") != null)) {

				String idClientS = (String) req.getParameter("ident");

				if ((idClientS != null) && (!idClientS.equals(""))) {
					try {
						Integer idClient = Integer.parseInt(idClientS);
						connected = metier.connecter(idClient);
						session.setAttribute("id_client", idClient);
						if (!connected) {
							req.setAttribute("erreur_connexion",
									"Identifiant invalide.");
						}
					} catch (NumberFormatException e) {
						connected = false;
						req.setAttribute("erreur_connexion",
								"L'identifiant ne doit contenir que des chiffres.");
					}
				} else {
					connected = false;
					req.setAttribute("erreur_connexion",
							"Veuillez saisir un identifiant.");
				}

			}
			/* inscription */
			else if (req.getParameter("inscription") != null) {

				// Metier demander inscription
				int idclient = metier.inscrire(req.getParameter("nom"),
						req.getParameter("prenom"), req.getParameter("numero"),
						req.getParameter("adresse"),
						req.getParameter("codepostal"),
						req.getParameter("ville"));

				if (idclient > 0) {
					session.setAttribute("id_client", idclient);
					connected = true;
				} else {
					req.setAttribute("erreur_connexion",
							"Veuillez remplir tout les champs.");
					req.setAttribute("nom", req.getParameter("nom"));
					req.setAttribute("prenom", req.getParameter("prenom"));
					req.setAttribute("numero", req.getParameter("numero"));
					req.setAttribute("adresse", req.getParameter("adresse"));
					req.setAttribute("codepostal",
							req.getParameter("codepostal"));
					req.setAttribute("ville", req.getParameter("ville"));
				}

			}

		} else {
			connected = true;
		}

		if (connected) {
			/*
			 * Cas : login ok suite à commander Afficher lien retour panier
			 */
			String provenance = (String) session.getAttribute("provenance");
			if (provenance != null) {
				if (provenance.equals("panier")) {
					retour = "panier";
				} else if (provenance.equals("historique")) {
					retour = "historique";
				} else {
					req.setAttribute("retour", "-");
				}
			}

		}

		return retour;
	}
}
